﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 二分查找
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { -1,0,3,5,9,12 };

            Console.WriteLine(ErFind(array, 9));
            Console.ReadKey();
        }

        public static int ErFind(int[] nums, int target)
        {
            int left = 0, right = nums.Length - 1;

            while (left<=right)
            {
                int mid = left + (right - left) / 2;
                if (nums[mid] == target) return mid;
                else if (nums[mid] > target) right = mid - 1;
                else left = mid + 1;
            }
            return -1;
        }
    }
}
